#!/usr/bin/env python
# coding: utf-8

# In[2]:


import pandas as pd
pd.plotting.register_matplotlib_converters()
import matplotlib.pyplot as plt
get_ipython().run_line_magic('matplotlib', 'inline')
import seaborn as sns
import matplotlib.dates as mdates
import numpy as np # linear algebra
from sklearn.metrics import mean_absolute_error, mean_squared_error
from sklearn.model_selection import train_test_split
import math   
from datetime import datetime, date 
import warnings
warnings.filterwarnings('ignore')
from tensorflow import keras
from tensorflow.keras import layers
from keras.models import Sequential
from keras.layers import Dense

print("Setup Complete")


# In[3]:


dam = pd.read_csv("3Dam.csv", encoding= 'cp949')
dam.head()


# In[4]:


dam.columns = ['date', 'water_level', 'rainfall', 'inflow', 'outflow', 'water_level2', 'rainfall2', 'inflow2', 'outflow2','water_level3', 'rainfall3', 'inflow3', 'outflow3']
dam.head()


# In[5]:


dam['inflow'] = pd.to_numeric(dam['inflow'],errors='coerce')
dam['outflow'] = pd.to_numeric(dam['outflow'],errors='coerce')
dam['inflow2'] = pd.to_numeric(dam['inflow2'],errors='coerce')
dam['outflow2'] = pd.to_numeric(dam['outflow2'],errors='coerce')
dam['inflow3'] = pd.to_numeric(dam['inflow3'],errors='coerce')
dam['outflow3'] = pd.to_numeric(dam['outflow3'],errors='coerce')


# In[6]:


print(dam.isnull().sum())


# In[7]:


dam = dam.dropna(axis=0)


# In[8]:


print(dam.isnull().sum())


# In[9]:


dam.describe()


# In[10]:


dam=dam[dam['water_level']>100]
dam=dam[dam['rainfall']>=0]
dam=dam[dam['inflow']>=0]
dam=dam[dam['outflow']>=0]

dam=dam[dam['rainfall2']>=0]
dam=dam[dam['inflow2']>=0]
dam=dam[dam['outflow2']>=0]

dam=dam[dam['water_level3']>0]
dam=dam[dam['rainfall3']>=0]
dam=dam[dam['inflow3']>=0]
dam=dam[dam['outflow3']>=0]


# In[11]:


dam.describe()


# In[13]:


dam = dam.reset_index(drop=True)


# # Remove Outlier

# In[14]:


plt.figure(figsize=(25,6))
sns.boxplot(data=dam['outflow'], color='red')
plt.show()


# In[15]:


#remove outlier

Q1 = dam['outflow'].quantile(0.25)
Q3 = dam['outflow'].quantile(0.75)
IQR = Q3 - Q1    #IQR is interquartile range. 

filter = (dam['outflow'] >= Q1 - 1.5 * IQR) & (dam['outflow'] <= Q3 + 1.5 *IQR)
dam = dam.loc[filter]
print(dam['outflow'].describe())
print('\n')
print(dam['outflow'].describe())


# In[16]:


plt.figure(figsize=(25,6))
sns.boxplot(data=dam['outflow'], color='red')
plt.show()


# In[17]:


dam = dam.reset_index(drop=True)


# In[18]:


dam["date"] = pd.to_datetime(dam["date"])


# In[19]:


dam.index = dam["date"]


# # Normalize

# In[22]:


dam


# In[23]:


features = dam[['water_level', 'rainfall', 'inflow', 'outflow', 'water_level2', 'rainfall2', 'inflow2', 'outflow2','water_level3', 'rainfall3', 'inflow3', 'outflow3']]
print(features)


# In[24]:


from sklearn.preprocessing import MinMaxScaler

scaler=MinMaxScaler()
scaler.fit(features)
dam_scaled=scaler.transform(features)

dam_scaled=pd.DataFrame(data=dam_scaled, columns=list(features))
print('feature 최솟값')
print(dam_scaled.min())
print('\n''fearure 최댓값')
print(dam_scaled.max())


# In[25]:


print(dam_scaled)


# In[26]:


dam['variation'] = 0
for i, idx in enumerate(dam.index):
    dam.loc[idx, 'variation'] = dam_scaled['rainfall'][i]+dam_scaled['inflow'][i]-dam_scaled['outflow'][i]


# In[27]:


dam['variation2'] = 0
for i, idx in enumerate(dam.index):
    dam.loc[idx, 'variation2'] = dam_scaled['rainfall2'][i]+dam_scaled['inflow2'][i]-dam_scaled['outflow2'][i]


# In[28]:


dam['variation3'] = 0
for i, idx in enumerate(dam.index):
    dam.loc[idx, 'variation3'] = dam_scaled['rainfall3'][i]+dam_scaled['inflow3'][i]-dam_scaled['outflow3'][i]


# In[29]:


df = dam['variation']
df


# In[30]:


ds_list = []

for i in range(1,len(df)):
    ds = df[i]-df[i-1]
    ds_list.append(ds)
    
ds_dt = pd.DataFrame(ds_list)


# In[31]:


ds_dt.columns = ['ds_dt']
print(ds_dt)


# In[32]:


ds_dt['ds_dt'] = pd.to_numeric(ds_dt['ds_dt'],errors='coerce')
print(ds_dt)


# In[33]:


df2 = dam['variation2']
df2


# In[34]:


ds_list = []

for i in range(1,len(df2)):
    ds = df2[i]-df2[i-1]
    ds_list.append(ds)
    
ds_dt2 = pd.DataFrame(ds_list)


# In[35]:


ds_dt2.columns = ['ds_dt2']
print(ds_dt2)


# In[36]:


ds_dt2['ds_dt2'] = pd.to_numeric(ds_dt2['ds_dt2'],errors='coerce')
print(ds_dt2)


# In[37]:


df3 = dam['variation3']
df3


# In[38]:


ds_list = []

for i in range(1,len(df)):
    ds = df3[i]-df3[i-1]
    ds_list.append(ds)
    
ds_dt3 = pd.DataFrame(ds_list)


# In[39]:


ds_dt3.columns = ['ds_dt3']
print(ds_dt3)


# In[40]:


ds_dt3['ds_dt3'] = pd.to_numeric(ds_dt3['ds_dt3'],errors='coerce')
print(ds_dt3)


# In[41]:


dam = dam[1:]


# In[42]:


dam = dam.reset_index(drop=True)
print(dam)


# In[43]:


dam['ds_dt'] = ds_dt
dam['ds_dt2'] = ds_dt2
dam['ds_dt3'] = ds_dt3


# In[44]:


dam = dam.reset_index(drop=True)
print(dam)


# In[45]:


dam["date"] = pd.to_datetime(dam["date"])


# In[46]:


dam.index = dam["date"]


# # GRU model

# In[47]:


dam.describe()


# In[48]:


168304*0.8


# In[49]:


train=dam[:134643]
test=dam[134643:168304]


# In[50]:


def make_dataset(data, label, window_size=144):
    feature_list = []
    label_list = []
    for i in range(len(data) - window_size):
        feature_list.append(np.array(data.iloc[i:i+window_size]))
        label_list.append(np.array(label.iloc[i+window_size]))
    return np.array(feature_list), np.array(label_list)


# In[51]:


feature_cols = ['rainfall', 'inflow', 'outflow', 'ds_dt']
label_cols = ['water_level']

train_feature = train[feature_cols]
train_label = train[label_cols]

test_feature = test[feature_cols]
test_label = test[label_cols]


# In[52]:


x_train, y_train = make_dataset(train_feature,train_label,144)


# In[53]:


x_test, y_test = make_dataset(test_feature,test_label,144)


# In[107]:


test_feature.shape


# In[54]:


from tensorflow.keras.models import load_model
model_s1d = load_model('model_s1d.h5')


# # TRAIN

# In[59]:


plt.figure(figsize=(10,8))
predict_train  = model_s1d.predict(x_train)
y = y_train
plt.plot(y_train, y)
plt.plot(y_train,predict_train, 'ro',alpha = 0.1)
plt.xlabel('Actual water level [EL.m]', fontsize =27)
plt.ylabel('Predicted water level [EL.m]', fontsize =27)
plt.xticks(fontsize =18)
plt.yticks(fontsize =18)
plt.xlim
plt.ylim
plt.show()


# In[62]:


import hydroeval as he
nse = he.evaluator(he.nse, predict_train, y)
kge, r, alpha, beta = he.evaluator(he.kge, predict_train, y)

print(nse, kge)


# In[56]:


from sklearn.metrics import mean_absolute_error

from sklearn.metrics import mean_squared_error

from sklearn.metrics import mean_squared_log_error

from sklearn.metrics import r2_score

from sklearn.metrics import mean_absolute_percentage_error


# In[57]:


print("MAE : " + str(mean_absolute_error(y, predict_train)))

print("MAPE : " + str(mean_absolute_percentage_error(y, predict_train)))

print("MSE : " + str(mean_squared_error(y, predict_train)))

# RMSE
print("RMSE = ", np.sqrt(mean_squared_error(y, predict_train)))

def rmsle(y, predict_train):
    return np.sqrt(np.mean(np.square(np.log1p(y) - np.log1p(predict_train))))

print("RMSLE = ", rmsle(y, predict_train))

print("R2 : " + str(r2_score(y, predict_train)))


# In[63]:


outputs_train2 = pd.DataFrame(y,columns = ['water level'])
outputs_train2['predicted water level'] = predict_train
print(outputs_train2)


# In[64]:


oo = dam[144:134643]


# In[65]:


Date=pd.DataFrame(oo.index)


# In[66]:


Date
Date['date'] = pd.to_datetime(Date.date)
print(Date)


# In[67]:


outputs_train3 = pd.concat([Date,outputs_train2],axis = 1)

outputs_train3


# In[68]:


outputs_train3.index = outputs_train3["date"]


# In[69]:


outputs_train3


# In[70]:


plt.figure(figsize=(17, 6))

pre = sns.lineplot(data=outputs_train3)

plt.ylabel('Water level [EL.m]',fontsize = 26)
#plt.xlabel('Date', fontsize =26)
plt.grid(True)
plt.legend(labels=["Actual water level", "Predicted water level"], fontsize=16)
plt.xticks(fontsize =18)
plt.yticks(fontsize =18)
#pre.xaxis.set_major_locator(mdates.YearLocator())
#pre.xaxis.set_major_formatter(mdates.DateFormatter('%Y'))
plt.show()


# In[71]:


rev_normal_tr = pd.DataFrame(predict_train)
rev_normal_tr = rev_normal_tr.ewm(span=6*24).mean()


# In[72]:


outputs_train4 = pd.DataFrame(y,columns = ['water level obs'])
outputs_train4['water level pre'] = rev_normal_tr
print(outputs_train4)


# In[73]:


outputs_train4 = pd.concat([Date,outputs_train4],axis = 1)

outputs_train4.index = outputs_train4["date"]


# In[74]:


plt.figure(figsize=(17, 6))

pre = sns.lineplot(data=outputs_train4, linewidth=3)

plt.ylabel('Water level [EL.m]',fontsize = 26)
#plt.xlabel('Date', fontsize =26)
plt.grid(True)
plt.legend(labels=["Actual water level", "Predicted water level"], fontsize=16)
plt.xticks(fontsize =18)
plt.yticks(fontsize =18)
plt.show()


# In[75]:


plt.figure(figsize=(10,8))
predict_train  = model_s1d.predict(x_train)
y = y_train
plt.plot(y_train, y)
plt.plot(y_train,rev_normal_tr, 'ro',alpha = 0.1)
plt.xlabel('Actual water level [EL.m]', fontsize =27)
plt.ylabel('Predicted water level [EL.m]', fontsize =27)
plt.xticks(fontsize =18)
plt.yticks(fontsize =18)
plt.xlim
plt.ylim
plt.show()


# In[76]:


import hydroeval as he
nse = he.evaluator(he.nse, rev_normal_tr, y)
kge, r, alpha, beta = he.evaluator(he.kge, rev_normal_tr, y)

print(nse, kge)


# In[77]:


print("MAE : " + str(mean_absolute_error(y, rev_normal_tr)))

print("MAPE : " + str(mean_absolute_percentage_error(y, rev_normal_tr)))

print("MSE : " + str(mean_squared_error(y, rev_normal_tr)))

# RMSE
print("RMSE = ", np.sqrt(mean_squared_error(y, rev_normal_tr)))

print("R2 : " + str(r2_score(y, rev_normal_tr)))


# In[77]:


#outputs_train4.to_csv('s1t_train.csv')


# In[78]:


#dam.to_csv('dam_s1.csv')


# # TEST

# In[103]:


plt.figure(figsize=(10,8))
predict_test  = model_s1d.predict(x_test)
y = y_test
plt.plot(y_test, y)
plt.plot(y_test,predict_test, 'ro',alpha = 0.1)
plt.xlabel('Actual water level [EL.m]', fontsize =27)
plt.ylabel('Predicted water level [EL.m]', fontsize =27)
plt.xticks(fontsize =18)
plt.yticks(fontsize =18)
plt.xlim
plt.ylim
plt.show()


# In[104]:


import hydroeval as he
nse = he.evaluator(he.nse, predict_test, y)
kge, r, alpha, beta = he.evaluator(he.kge, predict_test, y)

print(nse, kge)


# In[105]:


from sklearn.metrics import mean_absolute_error

from sklearn.metrics import mean_squared_error

from sklearn.metrics import mean_squared_log_error

from sklearn.metrics import r2_score

from sklearn.metrics import mean_absolute_percentage_error


# In[106]:


print("MAE : " + str(mean_absolute_error(y, predict_test)))

print("MAPE : " + str(mean_absolute_percentage_error(y, predict_test)))

print("MSE : " + str(mean_squared_error(y, predict_test)))

# RMSE
print("RMSE = ", np.sqrt(mean_squared_error(y, predict_test)))

def rmsle(y, predict_test):
    return np.sqrt(np.mean(np.square(np.log1p(y) - np.log1p(predict_test))))

print("RMSLE = ", rmsle(y, predict_test))

print("R2 : " + str(r2_score(y, predict_test)))


# In[107]:


outputs_test2 = pd.DataFrame(y,columns = ['water level'])
outputs_test2['predicted water level'] = predict_test
print(outputs_test2)


# In[108]:


134643+144


# In[109]:


oo = dam[134787:168304]
Date=pd.DataFrame(oo.index)


# In[110]:


Date=pd.DataFrame(oo.index)


# In[111]:


Date
Date['date'] = pd.to_datetime(Date.date)
print(Date)


# In[112]:


outputs_test2


# In[113]:


outputs_test3 = pd.concat([Date,outputs_test2],axis = 1)

outputs_test3


# In[114]:


outputs_test3.index = outputs_test3["date"]


# In[115]:


outputs_test3


# In[116]:


plt.figure(figsize=(17, 6))

pre = sns.lineplot(data=outputs_test3)

plt.ylabel('Water level [EL.m]',fontsize = 26)
#plt.xlabel('Date', fontsize =26)
plt.grid(True)
plt.legend(labels=["Actual water level", "Predicted water level"], fontsize=16)
plt.xticks(fontsize =18)
plt.yticks(fontsize =18)
#pre.xaxis.set_major_locator(mdates.YearLocator())
#pre.xaxis.set_major_formatter(mdates.DateFormatter('%Y'))
plt.show()


# In[117]:


#outputs_test3.to_csv('s1_before_ma.csv')


# In[95]:


rev_normal_test = pd.DataFrame(predict_test)
rev_normal_test = rev_normal_test.ewm(span=6*24).mean()


# In[96]:


outputs_test4 = pd.DataFrame(y,columns = ['water level obs'])
outputs_test4['water level pre'] = rev_normal_test
print(outputs_test4)


# In[97]:


outputs_test4 = pd.concat([Date,outputs_test4],axis = 1)

outputs_test4.index = outputs_test4["date"]


# In[98]:


outputs_test4


# In[99]:


plt.figure(figsize=(17, 6))

pre = sns.lineplot(data=outputs_test4, linewidth=3)

plt.ylabel('Water level [EL.m]',fontsize = 26)
#plt.xlabel('Date', fontsize =26)
plt.grid(True)
plt.legend(labels=["Actual water level", "Predicted water level"], fontsize=16)
plt.xticks(fontsize =18)
plt.yticks(fontsize =18)
plt.show()


# In[100]:


plt.figure(figsize=(10,8))
predict_test  = model_s1d.predict(x_test)
y = y_test
plt.plot(y_test, y)
plt.plot(y_test,rev_normal_test, 'ro',alpha = 0.1)
plt.xlabel('Actual water level [EL.m]', fontsize =27)
plt.ylabel('Predicted water level [EL.m]', fontsize =27)
plt.xticks(fontsize =18)
plt.yticks(fontsize =18)
plt.xlim
plt.ylim
plt.show()


# In[101]:


import hydroeval as he


# In[102]:


nse = he.evaluator(he.nse, rev_normal_test, y)
kge, r, alpha, beta = he.evaluator(he.kge, rev_normal_test, y)

print(nse, kge)


# In[101]:


print("MAE : " + str(mean_absolute_error(y, rev_normal_test)))

print("MAPE : " + str(mean_absolute_percentage_error(y, rev_normal_test)))

print("MSE : " + str(mean_squared_error(y, rev_normal_test)))

# RMSE
print("RMSE = ", np.sqrt(mean_squared_error(y, rev_normal_test)))

print("R2 : " + str(r2_score(y, rev_normal_test)))


# In[102]:


#outputs_test4.to_csv('s1t.csv')


# # Scenario 

# # Inflow

# In[276]:


first=dam[134643:168304]


# In[277]:


first


# In[278]:


first['inflow'] = first['inflow']*0.6


# In[279]:


features = first[['water_level', 'rainfall', 'inflow', 'outflow', 'water_level2', 'rainfall2', 'inflow2', 'outflow2','water_level3', 'rainfall3', 'inflow3', 'outflow3']]
print(features)


# In[280]:


from sklearn.preprocessing import MinMaxScaler

scaler=MinMaxScaler()
scaler.fit(features)
dam_scaled=scaler.transform(features)

dam_scaled=pd.DataFrame(data=dam_scaled, columns=list(features))
print('feature 최솟값')
print(dam_scaled.min())
print('\n''fearure 최댓값')
print(dam_scaled.max())


# In[281]:


first['variation'] = 0
for i, idx in enumerate(first.index):
    first.loc[idx, 'variation'] = dam_scaled['rainfall'][i]+dam_scaled['inflow'][i]-dam_scaled['outflow'][i]


# In[282]:


df2 = first['variation']
df2


# In[283]:


df2 = df2.reset_index(drop=True)


# In[284]:


ds_list = []

for i in range(1,len(df2)):
    ds = df2[i]-df2[i-1]
    ds_list.append(ds)
    
ds_dt = pd.DataFrame(ds_list)


# In[285]:


ds_dt.columns = ['ds_dt']
print(ds_dt)


# In[286]:


ds_dt['ds_dt'] = pd.to_numeric(ds_dt['ds_dt'],errors='coerce')
print(ds_dt)


# In[287]:


first = first[1:]


# In[288]:


first = first.reset_index(drop=True)


# In[289]:


first['ds_dt'] = ds_dt


# In[290]:


first = first.reset_index(drop=True)


# In[291]:


orginal_out1= predict_test[1:]


# In[292]:


train_feature1 = first[feature_cols]
train_label1 = first[label_cols]


# In[293]:


x_rcheck1, y_rcheck1 = make_dataset(train_feature1,train_label1,144)


# In[294]:


sin_sm2a = model_s1d.predict(x_rcheck1)


# In[295]:


obs = dam.water_level[134788:168304].to_numpy()


# In[296]:


obs = pd.DataFrame(obs,columns = ['Observation']) 
obs['Original prediction'] = orginal_out1
obs['Scenario : 0.6 Inflow'] = sin_sm2a


# In[297]:


outputs_plot1 = pd.DataFrame(obs) #plot2,t2


# In[298]:


outputs_plot1


# In[299]:


oo = dam[134788:168304]


# In[300]:


Date=pd.DataFrame(oo.index)


# In[301]:


outputs_plot1 = pd.concat([Date,obs],axis = 1)


# In[302]:


outputs_plot1.index = outputs_plot1["date"]


# In[303]:


outputs_plot1


# In[304]:


plt.figure(figsize=(20, 6))

pre = sns.lineplot(data=outputs_plot1)#, palette=['dodgerblue', 'orange', 'red'])

plt.ylabel('Water Level [EL.m]', fontsize =25)
plt.xlim
plt.ylim
plt.grid(True)  
plt.legend(fontsize =19)
plt.xticks(fontsize =18)
plt.yticks(fontsize =18)
plt.show()


# In[305]:


original_pre_169 = pd.DataFrame(orginal_out1)
original_pre_169 = original_pre_169.ewm(span=24*6).mean()


# In[306]:


sin1_169 = pd.DataFrame(sin_sm2a)
sin1_169 = sin1_169.ewm(span=24*6).mean()


# In[307]:


obs['Original prediction'] = original_pre_169
obs['Scenario : 0.6 Inflow'] = sin1_169
print(obs)


# In[308]:


outputs_test4 = pd.concat([Date,obs],axis = 1)

outputs_test4


# In[309]:


outputs_test4.index = outputs_test4["date"]


# In[310]:


plt.figure(figsize=(20, 6))

pre = sns.lineplot(data=outputs_test4, linewidth=3)

plt.ylabel('Water level [EL.m]',fontsize = 25)
plt.xlabel('Date', fontsize =17)
plt.title('Seomjin dam\n', fontsize =25)
plt.grid(True)  
plt.legend(fontsize =18)
plt.xticks(fontsize =18)
plt.yticks(fontsize =18)
plt.show()


# In[311]:


#outputs_test4.to_csv('s1d_0.6_inflow.csv')


# # Rainfall

# In[411]:


first=dam[134643:168304]


# In[412]:


first['rainfall'] = first['rainfall']*1.4


# In[413]:


features = first[['water_level', 'rainfall', 'inflow', 'outflow', 'water_level2', 'rainfall2', 'inflow2', 'outflow2','water_level3', 'rainfall3', 'inflow3', 'outflow3']]
print(features)


# In[414]:


from sklearn.preprocessing import MinMaxScaler

scaler=MinMaxScaler()
scaler.fit(features)
dam_scaled=scaler.transform(features)

dam_scaled=pd.DataFrame(data=dam_scaled, columns=list(features))
print('feature 최솟값')
print(dam_scaled.min())
print('\n''fearure 최댓값')
print(dam_scaled.max())


# In[415]:


first['variation'] = 0
for i, idx in enumerate(first.index):
    first.loc[idx, 'variation'] = dam_scaled['rainfall'][i]+dam_scaled['inflow'][i]-dam_scaled['outflow'][i]


# In[416]:


df2 = first['variation']
df2


# In[417]:


df2 = df2.reset_index(drop=True)


# In[418]:


ds_list = []

for i in range(1,len(df2)):
    ds = df2[i]-df2[i-1]
    ds_list.append(ds)
    
ds_dt = pd.DataFrame(ds_list)


# In[419]:


ds_dt.columns = ['ds_dt']
print(ds_dt)


# In[420]:


ds_dt['ds_dt'] = pd.to_numeric(ds_dt['ds_dt'],errors='coerce')
print(ds_dt)


# In[421]:


first = first[1:]


# In[422]:


first = first.reset_index(drop=True)


# In[423]:


first['ds_dt'] = ds_dt


# In[424]:


first = first.reset_index(drop=True)


# In[425]:


orginal_out1= predict_test[1:]


# In[426]:


train_feature1 = first[feature_cols]
train_label1 = first[label_cols]


# In[427]:


x_rcheck1, y_rcheck1 = make_dataset(train_feature1,train_label1,144)


# In[428]:


sin_sm2a = model_s1d.predict(x_rcheck1)


# In[429]:


obs = dam.water_level[134788:168304].to_numpy()


# In[430]:


obs = pd.DataFrame(obs,columns = ['Observation']) 
obs['Original prediction'] = orginal_out1
obs['Scenario : 1.4 Rainfall'] = sin_sm2a


# In[431]:


outputs_plot1 = pd.DataFrame(obs) #plot2,t2
outputs_plot1


# In[432]:


oo = dam[134788:168304]


# In[433]:


Date=pd.DataFrame(oo.index)


# In[434]:


outputs_plot1 = pd.concat([Date,obs],axis = 1)


# In[435]:


outputs_plot1.index = outputs_plot1["date"]


# In[436]:


plt.figure(figsize=(20, 6))

pre = sns.lineplot(data=outputs_plot1)#, palette=['dodgerblue', 'orange', 'red'])

plt.ylabel('Water Level [EL.m]', fontsize =25)
plt.xlim
plt.ylim
plt.grid(True)  
plt.legend(fontsize =19)
plt.xticks(fontsize =18)
plt.yticks(fontsize =18)
plt.show()


# In[437]:


original_pre_169 = pd.DataFrame(orginal_out1)
original_pre_169 = original_pre_169.ewm(span=24*6).mean()


# In[438]:


sin1_169 = pd.DataFrame(sin_sm2a)
sin1_169 = sin1_169.ewm(span=24*6).mean()


# In[439]:


obs['Original prediction'] = original_pre_169
obs['Scenario : 1.4 Rainfall'] = sin1_169
print(obs)


# In[440]:


outputs_test4 = pd.concat([Date,obs],axis = 1)

outputs_test4


# In[441]:


outputs_test4.index = outputs_test4["date"]


# In[442]:


plt.figure(figsize=(20, 6))

pre = sns.lineplot(data=outputs_test4, linewidth=3)

plt.ylabel('Water level [EL.m]',fontsize = 25)
plt.xlabel('Date', fontsize =17)
plt.title('Seomjin dam\n', fontsize =25)
plt.grid(True)  
plt.legend(fontsize =18)
plt.xticks(fontsize =18)
plt.yticks(fontsize =18)
plt.show()


# In[443]:


#outputs_test4.to_csv('s1d_1.4_rainfall.csv')


# # Outflow

# In[123]:


first=dam[134643:168304]


# In[124]:


first


# In[125]:


first['outflow'] = first['outflow']*1.4


# In[126]:


features = first[['water_level', 'rainfall', 'inflow', 'outflow', 'water_level2', 'rainfall2', 'inflow2', 'outflow2','water_level3', 'rainfall3', 'inflow3', 'outflow3']]
print(features)


# In[127]:


from sklearn.preprocessing import MinMaxScaler

scaler=MinMaxScaler()
scaler.fit(features)
dam_scaled=scaler.transform(features)

dam_scaled=pd.DataFrame(data=dam_scaled, columns=list(features))
print('feature 최솟값')
print(dam_scaled.min())
print('\n''fearure 최댓값')
print(dam_scaled.max())


# In[128]:


first['variation'] = 0
for i, idx in enumerate(first.index):
    first.loc[idx, 'variation'] = dam_scaled['rainfall'][i]+dam_scaled['inflow'][i]-dam_scaled['outflow'][i]


# In[561]:


df2 = first['variation']
df2


# In[562]:


df2 = df2.reset_index(drop=True)


# In[563]:


ds_list = []

for i in range(1,len(df2)):
    ds = df2[i]-df2[i-1]
    ds_list.append(ds)
    
ds_dt = pd.DataFrame(ds_list)


# In[564]:


ds_dt.columns = ['ds_dt']
print(ds_dt)


# In[565]:


ds_dt['ds_dt'] = pd.to_numeric(ds_dt['ds_dt'],errors='coerce')
print(ds_dt)


# In[566]:


first = first[1:]


# In[567]:


first = first.reset_index(drop=True)


# In[568]:


first['ds_dt'] = ds_dt


# In[569]:


first = first.reset_index(drop=True)


# In[570]:


orginal_out1= predict_test[1:]


# In[571]:


train_feature1 = first[feature_cols]
train_label1 = first[label_cols]


# In[572]:


x_rcheck1, y_rcheck1 = make_dataset(train_feature1,train_label1,144)


# In[573]:


sin_sm2a = model_s1d.predict(x_rcheck1)


# In[574]:


134643+144


# In[575]:


obs = dam.water_level[134788:168304].to_numpy()


# In[576]:


obs = pd.DataFrame(obs,columns = ['observation']) 
obs['original prediction'] = orginal_out1
obs['Scenario : 0.8 outflow'] = sin_sm2a


# In[577]:


outputs_plot1 = pd.DataFrame(obs) #plot2,t2


# In[578]:


outputs_plot1


# In[579]:


oo = dam[134788:168304]


# In[580]:


Date=pd.DataFrame(oo.index)


# In[581]:


outputs_plot1 = pd.concat([Date,obs],axis = 1)


# In[582]:


outputs_plot1.index = outputs_plot1["date"]


# In[583]:


outputs_plot1


# In[584]:


plt.figure(figsize=(20, 6))

pre = sns.lineplot(data=outputs_plot1)#, palette=['dodgerblue', 'orange', 'red'])

plt.ylabel('Water Level [EL.m]', fontsize =25)
plt.xlim
plt.ylim
plt.grid(True)  
plt.legend(fontsize =19)
plt.xticks(fontsize =18)
plt.yticks(fontsize =18)
plt.show()


# In[585]:


original_pre_169 = pd.DataFrame(orginal_out1)
original_pre_169 = original_pre_169.ewm(span=24*6).mean()


# In[586]:


sin1_169 = pd.DataFrame(sin_sm2a)
sin1_169 = sin1_169.ewm(span=24*6).mean()


# In[587]:


obs['original prediction'] = original_pre_169
obs['Scenario : 0.8 outflow'] = sin1_169
print(obs)


# In[588]:


outputs_test4 = pd.concat([Date,obs],axis = 1)

outputs_test4


# In[589]:


outputs_test4.index = outputs_test4["date"]


# In[590]:


plt.figure(figsize=(20, 6))

pre = sns.lineplot(data=outputs_test4, linewidth=3)

plt.ylabel('Water level [EL.m]',fontsize = 25)
plt.xlabel('Date', fontsize =17)
plt.title('Seomjin dam\n', fontsize =25)
plt.grid(True)  
plt.legend(fontsize =18)
plt.xticks(fontsize =18)
plt.yticks(fontsize =18)
plt.show()


# In[591]:


#outputs_test4.to_csv('s1d_1.4.csv')

